﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using Automation.Protocols.Dao;
using Automation.Protocols.Model;
using System.Reflection;
using Driver.Const;

namespace Driver
{
    [Export(typeof(IDataAccess))]
    public class ExcelDataAccessPlugin:IDataAccess
    {
        private TestCaseBase _tcaseDAL;
        private TestCommandBase _tcommandDAL;
        private TestConfigBase _tconfigDAL;
        private TestDataBase _tdataDAL;
        private TestDatasetBase _tdatasetDAL;
        private TestPlanBase _tplanDAL;
        private TestProjectBase _tprojectDAL;
        private TestStepBase _tstepDAL;
        private TestMapBase _tmapDAL;
       
        public ExcelDataAccessPlugin()
        {
            string connectionString = ConfManager.DefaultExcelFile;

            string dataAccessObject = ConfManager.DefaultDataAccessObject;
            //Load assembly of Data access:
            //Current data access is Automation.Dal.Excel:
            // D:\Kim\working\Automation\v16\v16\AutomationTest\Automation.Dal.Excel\bin\Release\Automation.Dal.Excel.dll
            Assembly assembly = Assembly.LoadFrom(dataAccessObject);
            Type TestCase = assembly.GetType("Automation.Dal.Excel.Dao.TestCaseDao"),
                 TestCommand = assembly.GetType("Automation.Dal.Excel.Dao.TestCommandDao"),
                 TestConfig = assembly.GetType("Automation.Dal.Excel.Dao.TestConfigDao"),
                 TestData = assembly.GetType("Automation.Dal.Excel.Dao.TestDataDao"),
                 TestDataset = assembly.GetType("Automation.Dal.Excel.Dao.TestDatasetDao"),
                 TestPlan = assembly.GetType("Automation.Dal.Excel.Dao.TestPlanDao"),
                 TestProject = assembly.GetType("Automation.Dal.Excel.Dao.TestProjectDao"),
                 TestStep = assembly.GetType("Automation.Dal.Excel.Dao.TestStepDao"),
                 TestMap = assembly.GetType("Automation.Dal.Excel.Dao.TestMapDao");

            _tcaseDAL = Activator.CreateInstance(TestCase, connectionString) as TestCaseBase;
            _tcommandDAL = Activator.CreateInstance(TestCommand, connectionString) as TestCommandBase;
            _tconfigDAL = Activator.CreateInstance(TestConfig, connectionString) as TestConfigBase;
            _tdataDAL = Activator.CreateInstance(TestData, connectionString) as TestDataBase;
            _tdatasetDAL = Activator.CreateInstance(TestDataset, connectionString) as TestDatasetBase;
            _tplanDAL = Activator.CreateInstance(TestPlan, connectionString) as TestPlanBase;
            _tprojectDAL = Activator.CreateInstance(TestProject, connectionString) as TestProjectBase;
            _tstepDAL = Activator.CreateInstance(TestStep, connectionString) as TestStepBase;
            _tmapDAL = Activator.CreateInstance(TestMap, connectionString) as TestMapBase;
            //log.Debug(_tcaseDAL.getTestString());
            //object c = Activator.CreateInstance(b, "abc");
        }

        public void GenerateCommands(List<TestCommand> Commands) {
            _tcommandDAL.GenerateCommands(Commands);
        }
        public void GenerateTestdatas(List<TestData> tdatas) {
            _tdataDAL.GenerateTestdatas(tdatas);
        }
        public List<TestCase> getTestcases() {
            return _tcaseDAL.getTestcases();
        }
        public List<TestCommand> getTestcommands() {
            return _tcommandDAL.getTestcommands();
        }
        public Dictionary<string, string> getTestConfig() {
            return _tconfigDAL.getTestConfig();
        }
        public List<TestConfig> getTestConfigs() {
            return _tconfigDAL.getTestConfigs();
        }
        public int getTestDataIndex() {
            return _tdataDAL.getTestDataIndex();
        }
        public List<TestData> getTestdatas() {
            return _tdataDAL.getTestdatas();
        }
        public List<TestDataset> getTestdatasets() {
            return _tdatasetDAL.getTestdatasets();
        }
        public List<TestStep> getTeststeps() {
            return _tstepDAL.getTeststeps();
        }
        public void RefreshTDatas() {
            _tdataDAL.RefreshTDatas();
        }
        public void UpdateDatas(List<TestData> Datas) {
            _tdataDAL.UpdateDatas(Datas);
        }
        public void UpdateSteps(List<TestStep> Steps) {
            _tstepDAL.UpdateSteps(Steps);
        }
        //TMap
        public List<TestMap> GetTMaps()
        {
            return _tmapDAL.getListTMap();
        }

        public void UpdateTMaps(List<TestMap> listMaps)
        {
            _tmapDAL.UpdateTMap(listMaps);
        }

        public void InsertTMaps(List<TestMap> listMaps)
        {
            _tmapDAL.InsertTMap(listMaps);
        }
    }
}
